package com.winit.openapi.tms.builder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.winit.tms.spi.booking.vo.BookingOrderDocumentDetailVo;

public class BookingOrderDocumentDetailBuilder {

    public static List<BookingOrderDocumentDetailVo> buildDocumentDetailList(JSONObject json) {

        List<BookingOrderDocumentDetailVo> list = new ArrayList<BookingOrderDocumentDetailVo>();
        if (json.containsKey("documentDetailsList")) {
            JSONArray inboundOrderList = json.getJSONArray("documentDetailsList");
            BookingOrderDocumentDetailVo documentDetailVo = null;
            JSONObject jsonObject = null;
            for (Object object : inboundOrderList) {
                jsonObject = new JSONObject();
                jsonObject = (JSONObject) object;
                documentDetailVo = new BookingOrderDocumentDetailVo();
                if (jsonObject.containsKey("documentNo")) {
                    documentDetailVo.setDocumentNo(jsonObject.getString("documentNo"));
                }
                if (jsonObject.containsKey("documentType")) {
                    documentDetailVo.setDocumentNo(jsonObject.getString("documentType"));
                }
                if (jsonObject.containsKey("bookingNo")) {
                    documentDetailVo.setBookingNo(jsonObject.getString("bookingNo"));
                }
                if (jsonObject.containsKey("warehouseCode")) {
                    documentDetailVo.setWarehouseCode(jsonObject.getString("warehouseCode"));
                }
                if (jsonObject.containsKey("warehouseName")) {
                    documentDetailVo.setWarehouseName(jsonObject.getString("warehouseName"));
                }
                if (jsonObject.containsKey("volume")) {
                    documentDetailVo.setVolume(jsonObject.getBigDecimal("volume"));
                }
                if (jsonObject.containsKey("weight")) {
                    documentDetailVo.setWeight(jsonObject.getBigDecimal("weight"));
                }
                if (jsonObject.containsKey("packageQty")) {
                    documentDetailVo.setPackageQty(jsonObject.getInteger("packageQty"));
                }
                if (jsonObject.containsKey("itemQty")) {
                    documentDetailVo.setItemQty(jsonObject.getInteger("itemQty"));
                }
                if (jsonObject.containsKey("merchandiseQty")) {
                    documentDetailVo.setMerchandiseQty(jsonObject.getInteger("merchandiseQty"));
                }
                if (jsonObject.containsKey("orderDate")) {
                    documentDetailVo.setOrderDate(jsonObject.getDate("orderDate"));
                }
                list.add(documentDetailVo);
            }
        }
        return list;
    }

    public static Map<String, Object> buildReturnDocumentDetail(BookingOrderDocumentDetailVo vo) {
        Map<String, Object> map = new HashMap<String, Object>();
        if (null == vo) {
            return map;
        }
        map.put("id", vo.getId());
        map.put("documentNo", vo.getDocumentNo());
        map.put("documentType", vo.getDocumentType());
        map.put("bookingNo", vo.getBookingNo());
        map.put("warehouseCode", vo.getWarehouseCode());
        map.put("warehouseName", vo.getWarehouseName());
        map.put("volume", vo.getVolume());
        map.put("weight", vo.getWeight());
        map.put("packageQty", vo.getPackageQty());
        map.put("itemQty", vo.getItemQty());
        map.put("merchandiseQty", vo.getMerchandiseQty());
        map.put("orderDate", vo.getOrderDate());
        return map;
    }
}
